
package jmenu.gui;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.Timer;


public class StatusPanel extends javax.swing.JPanel {

    private static final Logger logger = Logger.getLogger(StatusPanel.class.getName());
    private static final String RES = "/jmenu/resources/";
    private Timer statusTimer = new Timer(5000, new StatusListener());
    
    /** Creates new form StatusPanel */
    public StatusPanel() {
        initComponents();
        statusTimer.setRepeats(false);
        this.progressBar.setMinimum(0);
        this.progressBar.setMaximum(100);
    }

    /** Prints message to status bar
     * 
     * @param message text
     * @param printTime show timestamp before text
     * @param icon show icon with text. Use null for no icon.
     * @param addToLog whether the message should be logged
     */
    public void setStatusMessage(String message,ImageIcon icon)
    {
        this.statusMessageLabel.setIcon(icon);  
        this.statusMessageLabel.setText(message);
    }
    
    /** Hide current status message after specified time. If new status message
     *  is displayed in the meantime, this scheduled action is cancelled.
     * @param millis time in milliseconds. Use 0 or negative number to cancel the timer.
     */
    public void hideStatusMessageAfter(int millis)
    {
        if (millis <= 0) {
            statusTimer.stop();
            return;
        }
        statusTimer.setInitialDelay(millis);
        statusTimer.restart();
    }

    /** Tells main form whether it should display task busy icon */
    public void setTaskRunning(boolean b) {
        if (b == false) {
            statusAnimationLabel.setIcon(new ImageIcon(getClass().getResource(RES + "task-idle.png")));
        } else {
            statusAnimationLabel.setIcon(new ImageIcon(getClass().getResource(RES + "task-busy.gif")));
        }
    }

    /** Set progress on progress bar
     * Use null to any parameter if you don't want to set it
     */
    public void setProgress(Integer value, String text, Boolean stringPainted, Boolean visible) {
        if (value != null) {
            progressBar.setValue(value);
        }
        if (text != null) {
            progressBar.setString(text);
        }
        if (stringPainted != null) {
            progressBar.setStringPainted(stringPainted);
        }
        if (visible != null) {
            progressBar.setVisible(visible);
        }
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        statusAnimationLabel = new JLabel();
        progressBar = new JProgressBar();
        statusMessageLabel = new JLabel();

        setFocusable(false);
        setMaximumSize(new Dimension(520, 20));
        setMinimumSize(new Dimension(520, 20));
        setVerifyInputWhenFocusTarget(false);

        statusAnimationLabel.setFocusable(false);
        statusAnimationLabel.setMaximumSize(new Dimension(20, 20));
        statusAnimationLabel.setMinimumSize(new Dimension(20, 20));
        statusAnimationLabel.setPreferredSize(new Dimension(20, 20));

        progressBar.setMaximum(15);
        progressBar.setFocusable(false);
        progressBar.setString("oko");
        progressBar.setStringPainted(true);
        progressBar.setVisible(false);

        statusMessageLabel.setHorizontalAlignment(SwingConstants.LEFT);
        statusMessageLabel.setVerticalAlignment(SwingConstants.TOP);

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel, GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(progressBar, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addComponent(statusAnimationLabel, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                    .addGroup(Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(statusMessageLabel, GroupLayout.DEFAULT_SIZE, 18, Short.MAX_VALUE))
                    .addComponent(progressBar, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(statusAnimationLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    
    /** Hide all information in status message label */
    private class StatusListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            statusMessageLabel.setIcon(null);
            statusMessageLabel.setText(null);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JProgressBar progressBar;
    private JLabel statusAnimationLabel;
    private JLabel statusMessageLabel;
    // End of variables declaration//GEN-END:variables
}
